//
//  PerfectMessageViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/4.
//

#import "PerfectMessageViewController.h"
#import "UserRequest.h"
#import "ChooseSexView.h"
#import "ChooseAgeView.h"
#import "WeightViewController.h"
#import "AppDelegate.h"
#import "BaseNavigationController.h"
#import "BaseTabBarController.h"
#import "AddCarViewController.h"

#import "UserRequest.h"
#import "ChooseCityInfoView.h"

#import "Cycling-swift.h"
#import "MineRootViewController.h"

@interface PerfectMessageViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *nameF;
@property (nonatomic, strong) UITextField *codeF;
@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UITextField *ageF;
@property (nonatomic, strong) UIButton *ageBtn;
@property (nonatomic, strong) UITextField *sexF;
@property (nonatomic, strong) UIButton *sexBtn;
@property (nonatomic, strong) UITextField *weightF;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic, strong) UITextField *cityF;

@property (nonatomic) BOOL cityDataIsRequesting;
@property (nonatomic) BOOL cityDataHasRequest;
@property (nonatomic) void(^requestCityDataComplete)(void);
@property (nonatomic, strong) NSArray *addressDatas;

@property (nonatomic) NSInteger sex;

@end

@implementation PerfectMessageViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = CycLocalizedString(@"Finish registration", @"完善注册");
    self.sex = -2;
    [self configUI];
    
    [self reloadCityDatasComplete:nil];
}

- (void)ageBtnClicked:(UIButton *)sender {
    [ChooseAgeView chooseAgeWith:self Complection:^(NSString *age) {
        self.ageF.text = age;
        [NSObject.getKeyWindowRootViewController gq_removeBgView];
    }];
}

- (void)sexBtnClicked:(UIButton *)sender {
    [ChooseSexView chooseSexWith:self Complection:^(int sex) {
        self.sex = sex;
        if(sex == 0) {
            self.sexF.text = CycLocalizedString(@"Woman", @"女");
        } else if(sex == 1) {
            self.sexF.text = CycLocalizedString(@"Man", @"男");
        } else {
            self.sexF.text = CycLocalizedString(@"Other", @"未知");
        }
        [NSObject.getKeyWindowRootViewController gq_removeBgView];
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)configUI {
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.image = [UIImage imageNamed:@"路径"];
    [self.view addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(18 + self.navigationHeight);
        make.leading.equalTo(self.view.mas_leading).offset(24);
        make.trailing.equalTo(self.view.mas_trailing).offset(-24);
        make.height.offset(132*kHEIGHT_RATIO);
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = CycLocalizedString(@"Welcome to emojo", @"欢迎来到骑行");
    titleL.textColor = rgb(48, 48, 48);
    titleL.numberOfLines = 0;
    titleL.font = [UIFont systemFontOfSize:28];
    [self.view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(topImageView.mas_top).offset(12);
    }];
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = rgb(10, 211, 159);
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleL.mas_left);
        make.top.equalTo(titleL.mas_bottom).offset(5);
        make.width.offset(38);
        make.height.offset(4);
    }];
    
    UILabel *nameL = [[UILabel alloc] init];
    nameL.text =  CycLocalizedString(@"Name", @"");
    nameL.textColor = rgb(166, 166, 166);
    nameL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:nameL];
    CGFloat leftWidth = 80;
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(topImageView.mas_bottom).offset(5);
        make.height.offset(50);
        make.width.offset(leftWidth);
    }];
    
    UITextField *nameF = [[UITextField alloc] init];
    nameF.placeholder = CycLocalizedString(@"Input your Name", @"");
    nameF.keyboardType = UIKeyboardTypeDefault;
    nameF.textColor = rgbHex(0xA6A6A6,1.0);
    nameF.delegate = self;
    nameF.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameF.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:nameF];
    self.nameF = nameF;
    [nameF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameL.mas_right).offset(22);
        make.top.equalTo(topImageView.mas_bottom).offset(5);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(50);
    }];
    
    UIView *nameV = [[UIView alloc] init];
    nameV.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:nameV];
    [nameV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(31);
        make.top.equalTo(nameF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-31);
        make.height.offset(1);
    }];
    
    UILabel *ageL = [[UILabel alloc] init];
    ageL.text = CycLocalizedString(@"Age", @"年龄");
    ageL.textColor = rgb(166, 166, 166);
    ageL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:ageL];
    [ageL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(nameV.mas_bottom).offset(17);
        make.height.offset(50);
        make.width.offset(leftWidth);
    }];
    
    UITextField *ageF = [[UITextField alloc] init];
    ageF.placeholder = CycLocalizedString(@"Your age", @"请输入年龄");
    ageF.borderStyle = UITextBorderStyleNone;
    ageF.textColor = rgbHex(0xA6A6A6,1.0);
    ageF.clearButtonMode = UITextFieldViewModeWhileEditing;
    ageF.keyboardType = UIKeyboardTypeDefault;
    ageF.delegate = self;
    ageF.userInteractionEnabled = NO;
    [self.view addSubview:ageF];
    self.ageF = ageF;
    [ageF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ageL.mas_right).offset(22);
        make.top.equalTo(nameV.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIView *ageView = [[UIView alloc] init];
    ageView.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:ageView];
    [ageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(28);
        make.top.equalTo(ageF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *ageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ageBtn setImage:[UIImage imageNamed:@"image_open"] forState:UIControlStateNormal];
    [ageBtn addTarget:self action:@selector(ageBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ageBtn];
    self.ageBtn = ageBtn;
    [ageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.centerY.equalTo(ageF.mas_centerY);
        make.width.offset(46);
        make.height.offset(46);
    }];
    
    UILabel *sexL = [[UILabel alloc] init];
    sexL.text = CycLocalizedString(@"Gender", @"性别");
    sexL.textColor = rgb(166, 166, 166);
    sexL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:sexL];
    [sexL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(ageView.mas_bottom).offset(17);
        make.height.offset(50);
        make.width.offset(leftWidth);
    }];
    
    UITextField *sexF = [[UITextField alloc] init];
    sexF.placeholder = CycLocalizedString(@"Choose gender", @"请选择性别");
    sexF.textColor = rgbHex(0xA6A6A6,1.0);
    sexF.borderStyle = UITextBorderStyleNone;
    sexF.clearButtonMode = UITextFieldViewModeWhileEditing;
    sexF.keyboardType = UIKeyboardTypeDefault;
    sexF.delegate = self;
    sexF.userInteractionEnabled = NO;
    [self.view addSubview:sexF];
    self.sexF = sexF;
    [sexF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sexL.mas_right).offset(22);
        make.top.equalTo(ageF.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIView *sexView = [[UIView alloc] init];
    sexView.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:sexView];
    [sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(28);
        make.top.equalTo(sexF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UILabel *weightL = [[UILabel alloc] init];
    weightL.text = CycLocalizedString(@"Weight", @"");
    weightL.textColor = rgb(166, 166, 166);
    weightL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:weightL];
    [weightL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(sexView.mas_bottom).offset(17);
        make.height.offset(50);
        make.width.offset(leftWidth);
    }];
    
    UITextField *weightF = [[UITextField alloc] init];
    weightF.placeholder = CycLocalizedString(@"Input weight(lbs)", @"请输入体重(lbs)");
    weightF.textColor = rgbHex(0xA6A6A6,1.0);
    weightF.borderStyle = UITextBorderStyleNone;
    weightF.userInteractionEnabled = YES;
    weightF.clearButtonMode = UITextFieldViewModeWhileEditing;
    weightF.delegate = self;
    weightF.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:weightF];
    self.weightF = weightF;
    [weightF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weightL.mas_right).offset(22);
        make.top.equalTo(sexView.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIButton *weightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [weightBtn addTarget:self action:@selector(setWeight) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weightBtn];
    [weightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weightL.mas_right).offset(22);
        make.top.equalTo(sexView.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    UIView *weightView = [[UIView alloc] init];
    weightView.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:weightView];
    [weightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(28);
        make.top.equalTo(weightF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *sexBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sexBtn setImage:[UIImage imageNamed:@"image_open"] forState:UIControlStateNormal];
    
    [sexBtn addTarget:self action:@selector(sexBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sexBtn];
    self.sexBtn = sexBtn;
    [sexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.centerY.equalTo(sexF.mas_centerY);
        make.width.offset(46);
        make.height.offset(46);
    }];
    
    UILabel *cityLabel = [UILabel new];
    cityLabel.text = CycLocalizedString(@"City", "城市");
    cityLabel.textColor = rgb(166, 166, 166);
    cityLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:cityLabel];
    [cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left);
        make.top.equalTo(weightView.mas_bottom).offset(17);
        make.height.offset(50);
        make.width.offset(leftWidth);
    }];
    
    UITextField *cityF = [[UITextField alloc] init];
    cityF.placeholder = CycLocalizedString(@"Select city", @"请选择城市");
    cityF.textColor = rgbHex(0xA6A6A6,1.0);
    cityF.borderStyle = UITextBorderStyleNone;
    cityF.userInteractionEnabled = NO;
    cityF.clearButtonMode = UITextFieldViewModeWhileEditing;
    cityF.delegate = self;
    cityF.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:cityF];
    [cityF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cityLabel.mas_right).offset(22);
        make.top.equalTo(weightView.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    self.cityF = cityF;
    
    UIButton *cityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cityBtn setImage:[UIImage imageNamed:@"image_open"] forState:UIControlStateNormal];
    [cityBtn addTarget:self action:@selector(chooseCity) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cityBtn];
    [cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weightView.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.height.offset(50);
        make.width.mas_equalTo(46);
    }];
    
    UIView *cityView = [UIView new];
    cityView.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:cityView];
    [cityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(28);
        make.top.equalTo(cityF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setTitle:CycLocalizedString(@"Submit", @"") forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:24];
    [registerBtn addTarget:self action:@selector(perfectMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    self.registerBtn = registerBtn;
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(26);
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.top.equalTo(cityView.mas_bottom).offset(20);
        make.height.offset(62);
    }];
    
}

- (void)setWeight {
    WeightViewController *vc = [[WeightViewController alloc] init];
    vc.block = ^(NSString * _Nonnull weight) {
        if(weight.length > 0) {
            self.weightF.text = [NSString stringWithFormat:@"%@ lbs", weight];
        } else {
            self.weightF.text = @"";
        }
    };
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)chooseCity {
    [self reloadCityDatasComplete:^{
        [self showSelectCityView];
    }];
}

- (void)perfectMessage {
    
    if (IsStrEmpty(self.nameF.text)) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Input your Name", @"")];
        return;
    }
    if (IsStrEmpty(self.ageF.text)) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Your age", @"请输入年龄")];
        return;
    }
    if (self.sex == -2) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Choose gender", @"请选择性别")];
        return;
    }
    [self showLoadingViewWithText:@""];
    [UserRequest completeUserFileWithName:self.nameF.text age:self.ageF.text.intValue weight:self.weightF.text gender:self.sex region:self.cityF.text result:^(BOOL ret) {
        [self removeLoadingView];
        if (ret) {
            // 注册成功跳转添加车辆界面
//            AddCarViewController  *vc = [AddCarViewController new];
//            vc.isFirst = YES;
//            [self.navigationController pushViewController:vc animated:YES];
            appDelegate.window.rootViewController = [[BaseNavigationController alloc] initWithRootViewController:[[BaseTabBarController alloc] init]];
            [appDelegate.window makeKeyAndVisible];
            
            [self requestHealthAppAuthorizationOnComplete:^(BOOL granted, BOOL shouldOpenHealthApp) {
                if(shouldOpenHealthApp) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"x-apple-health://app/"] options:@{} completionHandler:^(BOOL success) {
                        [MineRootViewController postShowHaveBikeAlertNotification];
                    }];
                } else {
                    [MineRootViewController postShowHaveBikeAlertNotification];
                }
            }];
        }else {
            [self showErrorWithText:CycLocalizedString(@"Register fail", @"注册失败")];
        }
    }];
}

//- (void)textFieldDidEndEditing:(UITextField *)textField {

//    if (!IsStrEmpty(self.nameF.text) && !IsStrEmpty(self.sexF.text) && !IsStrEmpty(self.ageF.text) && !IsStrEmpty(self.weightF.text)) {
//        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
//    } else {
//        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_shense"] forState:UIControlStateNormal];
//    }
//}
@end

/**
 选择城市
 */
@implementation PerfectMessageViewController (SelectCity)

- (void)reloadCityDatasComplete:(void(^)(void))complete
{
    if(self.cityDataHasRequest) {
        if(complete) {
            complete();
        }
        return;
    }
    self.requestCityDataComplete = complete;
    if(self.cityDataIsRequesting) {
        return;
    }
    [UserRequest requestAddressesData:^(NSDictionary * _Nonnull datas) {
        NSArray *cityDatas = [[datas valueForKey:@"Location"] valueForKey:@"regioncolumns"];
        if(![cityDatas isKindOfClass:[NSArray class]]) {
            self.cityDataIsRequesting = NO;
            self.requestCityDataComplete = nil;
            return;
        }
        cityDatas = [cityDatas firstObject];
        if(![cityDatas isKindOfClass:[NSArray class]]) {
            self.cityDataIsRequesting = NO;
            self.requestCityDataComplete = nil;
            return;
        }
        self.addressDatas = cityDatas;
        self.cityDataHasRequest = YES;
        self.cityDataIsRequesting = NO;
        if(self.requestCityDataComplete) {
            self.requestCityDataComplete();
        }
        self.requestCityDataComplete = nil;
    }];
}

- (void)showSelectCityView
{
    [ChooseCityInfoView chooseAddressWith:self andDataArr:self.addressDatas complection:^(NSString *address) {
        self.cityF.text = address;
        [NSObject.getKeyWindowRootViewController gq_removeBgView];
    }];
}

@end
